//
//  MyActivityController.m
//  YiBaoStore
//
//  Created by 毛云河 on 2019/1/5.
//  Copyright © 2019 小河. All rights reserved.
//

#import "MyActivityController.h"
#import "MyActivityCell.h"
#import "ActivityModel.h"

@interface MyActivityController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation MyActivityController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNav];
    [self createTableView];
    self.dataArray = [NSMutableArray array];
    [self httpRequestData];
}

- (void)httpRequestNewData
{
    [self.dataArray removeAllObjects];
    [self httpRequestData];
}

- (void)httpRequestData
{
    __weak MyActivityController *myself = self;
    
    [HttpRequest formPostPath:ActivityList_API params:nil resultBlock:^(id Data, MsgModel *msgModel)
    {
        NSError *error;
        if (msgModel.status == 200)
        {
            NSLog(@"活动列表****%@",Data);
            NSArray *result = (NSArray *)Data[@"result"];
            for (NSDictionary *dic in result)
            {
                ActivityModel *model = [[ActivityModel alloc] initWithDictionary:dic error:&error];
                [self.dataArray addObject:model];
            }
            [myself.myTableView.mj_header endRefreshing];
            [myself.myTableView reloadData];
        }
        else {
            [myself.myTableView.mj_header endRefreshing];
        }
        
    }];
}

#pragma mark - 设置导航
- (void)setNav
{
    [self setNaviWith:@"我的活动"];
    UIView *lineView = [[UIView alloc] init];
    [self.view addSubview:lineView];
    lineView.frame = CGRectMake(0, self.bgNavView.bottom, SCREENWIDTH, 1);
    lineView.backgroundColor = RGBACOLOR(241, 241, 241, 1);
}

#pragma mark - 创建视图
- (void)createTableView
{
    if (@available(iOS 11.0, *))
    {
        self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.bgNavView.bottom, SCREENWIDTH, SCREENHEIGHT-self.bgNavView.bottom-BottomHeight_xs) style:UITableViewStyleGrouped];
    }
    else {
        self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.bgNavView.bottom, SCREENWIDTH, SCREENHEIGHT-self.bgNavView.bottom-BottomHeight) style:UITableViewStyleGrouped];
    }
    
    [self.view addSubview:self.myTableView];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.sectionHeaderHeight = 0.01f;
    self.myTableView.sectionFooterHeight = 10.f;
    self.myTableView.tableHeaderView = [self tableHeaderView];
    self.myTableView.tableFooterView = [self tableFooterView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[MyActivityCell class] forCellReuseIdentifier:@"MyActivityCell"];
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(httpRequestNewData)];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 220.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyActivityCell"];
    cell.lookBtn.indexPath = indexPath;
    [cell.lookBtn addTarget:self action:@selector(lookBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    ActivityModel *model = self.dataArray[indexPath.section];
    [cell setCellModel:model];
    return cell;
}

- (void)lookBtnClick:(UIButton *)sender
{
    ActivityModel *model = self.dataArray[sender.indexPath.section];
    WebViewController *webVC = [WebViewController new];
    webVC.urlString = model.guideUrl;
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExchangeController *change = [[ExchangeController alloc] init];
    [self.navigationController pushViewController:change animated:YES];
}

-(UIView *)tableHeaderView
{
    UIView *headerView  = [[UIView alloc]init];
    headerView.frame = CGRectMake(0, 0, SCREENWIDTH, 0.01);
    
    return headerView;
}

-(UIView *)tableFooterView
{
    UIView *footerView  = [[UIView alloc]init];
    footerView.frame = CGRectMake(0, 0, SCREENWIDTH, 0.01);
    
    return footerView;
}











@end
